# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Release

on:
  release:
    types: [published]

env:
  OTP_VERSION: '25.2.1'
  ELIXIR_VERSION: '1.14.5'

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    env:
      RELEASE_VERSION: 5.3.3
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: blockscout/blockscout

      - name: Build & Push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./docker/Dockerfile
          push: true
          cache-from: type=registry,ref=blockscout/blockscout:buildcache
          cache-to: type=registry,ref=blockscout/blockscout:buildcache,mode=max
          tags: blockscout/blockscout:latest, blockscout/blockscout:${{ env.RELEASE_VERSION }}
          platforms: |
            linux/amd64
            linux/arm64/v8
          build-args: |
            CACHE_EXCHANGE_RATES_PERIOD=
            API_V1_READ_METHODS_DISABLED=false
            DISABLE_WEBAPP=false
            API_V1_WRITE_METHODS_DISABLED=false
            CACHE_TOTAL_GAS_USAGE_COUNTER_ENABLED=
            ADMIN_PANEL_ENABLED=false
            DECODE_NOT_A_CONTRACT_CALLS=false
            MIXPANEL_URL=
            MIXPANEL_TOKEN=
            AMPLITUDE_URL=
            AMPLITUDE_API_KEY=
            CACHE_ADDRESS_WITH_BALANCES_UPDATE_INTERVAL=
            BLOCKSCOUT_VERSION=v${{ env.RELEASE_VERSION }}-beta
            RELEASE_VERSION=${{ env.RELEASE_VERSION }}

      - name: Build and push Docker image for zkEVM
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./docker/Dockerfile
          push: true
          tags: blockscout/blockscout-zkevm:latest, blockscout/blockscout-zkevm:${{ env.RELEASE_VERSION }}
          platforms: |
            linux/amd64
            linux/arm64/v8
          build-args: |
            CACHE_EXCHANGE_RATES_PERIOD=
            API_V1_READ_METHODS_DISABLED=false
            DISABLE_WEBAPP=false
            API_V1_WRITE_METHODS_DISABLED=false
            CACHE_TOTAL_GAS_USAGE_COUNTER_ENABLED=
            ADMIN_PANEL_ENABLED=false
            CACHE_ADDRESS_WITH_BALANCES_UPDATE_INTERVAL=
            BLOCKSCOUT_VERSION=v${{ env.RELEASE_VERSION }}-beta
            RELEASE_VERSION=${{ env.RELEASE_VERSION }}
            CHAIN_TYPE=polygon_zkevm

      - name: Build and push Docker image for SUAVE
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./docker/Dockerfile
          push: true
          tags: blockscout/blockscout-suave:latest, blockscout/blockscout-suave:${{ env.RELEASE_VERSION }}
          platforms: |
            linux/amd64
            linux/arm64/v8
          build-args: |
            CACHE_EXCHANGE_RATES_PERIOD=
            API_V1_READ_METHODS_DISABLED=false
            DISABLE_WEBAPP=false
            API_V1_WRITE_METHODS_DISABLED=false
            CACHE_TOTAL_GAS_USAGE_COUNTER_ENABLED=
            ADMIN_PANEL_ENABLED=false
            CACHE_ADDRESS_WITH_BALANCES_UPDATE_INTERVAL=
            BLOCKSCOUT_VERSION=v${{ env.RELEASE_VERSION }}-beta
            RELEASE_VERSION=${{ env.RELEASE_VERSION }}
            CHAIN_TYPE=suave

      - name: Send release announcement to Slack workflow
        id: slack
        uses: slackapi/slack-github-action@v1.24.0
        with:
          payload: |
            {
              "release-version": "${{ env.RELEASE_VERSION }}",
              "release-link": "https://github.com/blockscout/blockscout/releases/tag/v${{ env.RELEASE_VERSION }}-beta"
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

  # merge-master-after-release:
  #   name: Merge 'master' to specific branch after release
  #   runs-on: ubuntu-latest
  #   env:
  #     BRANCHES: |
  #       production-core
  #       production-sokol
  #       production-eth-experimental
  #       production-eth-goerli
  #       production-lukso
  #       production-xdai
  #       production-polygon-supernets
  #       production-rsk
  #       production-immutable
  #   steps:
  #   - uses: actions/checkout@v4
  #   - name: Set Git config
  #     run: |
  #         git config --local user.email "actions@github.com"
  #         git config --local user.name "Github Actions"
  #   - name: Merge master back after release
  #     run: |
  #         git fetch --unshallow
  #         touch errors.txt
  #         for branch in $BRANCHES;
  #         do
  #           git reset --merge
  #           git checkout master
  #           git fetch origin
  #           echo $branch
  #           git ls-remote --exit-code --heads origin $branch || { echo $branch >> errors.txt; continue; }
  #           echo "Merge 'master' to $branch"
  #           git checkout $branch
  #           git pull || { echo $branch >> errors.txt; continue; }
  #           git merge --no-ff master -m "Auto-merge master back to $branch" || { echo $branch >> errors.txt; continue; }
  #           git push || { echo $branch >> errors.txt; continue; }
  #           git checkout master;
  #         done
  #         [ -s errors.txt ] && echo "There are problems with merging 'master' to branches:" || echo "Errors file is empty"
  #         cat errors.txt
  #         [ ! -s errors.txt ]
